<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/10
 * Time: 20:41
 */

namespace app\lnadmin\model\admin;

use app\common\Common;
use app\lnadmin\controller\Upload;
use app\lnadmin\model\core\InitModel;
use app\lnadmin\model\admin\User as UserModel;
use think\model\concern\SoftDelete;
/**
 * 管理员
 * Class Admin
 * @package app\model\admin
 */
class Lnadmin extends InitModel
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    // 定义表
    protected $name = 'admin';

    // 第一自动写入时间字段
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    // 可用字段
    protected static $available = [
        'id',
        'group_id',
        'username',
        'nickname',
        'password',
        'salt',
        'email',
        'loginip',
        'loginfailure',
        'login_time',
        'create_time',
        'update_time',
        'token',
        'status',
    ];

    // 新增管理员
    public static function add($post)
    {
        if (isset($post['id'])) unset($post['id']);
        try {
            $model = new self();
            foreach ($post as $key => $val) {
                if (in_array($key, self::$available)) {
                    $model->$key = $val;
                }
            }
            $model->nickname = $post['username'];
            $model->token = Common::token();
            $model->salt = Common::pwdMd5($post['password'])['salt'];
            $model->password = Common::pwdMd5($post['password'])['password'];
            $model->save();

            // 保存图片
            if (!empty($post['image'])) {

                Image::create([
                    'table_id' => $model->id,
                    'url' => $post['image'],
                    'types' => Upload::TYPE_ADMIN,
                    'file_size' => $post['filesize'],
                    'mine' => $post['mine']
             ]);
            }
            // 写入权限组$post['filesize']
            if (!empty($post['group_id'])){
                AuthGroupAccess::add($post['group_id'],$model->id);
            }

            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
  
     // 检查登录
    public static function check($name)
    {   
        return self::with(['authGroupAccess'])->where(['username' => $name])->find();
    }

 
    public function authGroupAccess()
    {
        return $this->hasOne(AuthGroupAccess::class, 'uid');
    }


    // 检测token
    public static function checkToken($token)
    {
       return self::where(['token' => $token])->field(['id', 'group_id', 'username', 'token', 'prev_time', 'login_time'])->find();
    }

   
    /**
     * 编辑
     * @param $post
     * @return bool
     * @author: MK
     * @Time: 2020/4/5 12:49
     */
     public static function edit($post)
    {
        try {
            $id = (int)$post['id'];
            $model = self::where(['id' => $id])->find();
            if (!$model) return false;
            $model->save($post);  
         //保存图片
            if (!empty($post['image'])) {
             Image::add([
                    'table_id' => $id,
                    'url' => $post['image'],
                    'types' => Upload::TYPE_ADMIN,
                    'file_size' => $post['filesize'],
                    'mine' => $post['mine']
             ]);
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
    // 删除检查
    public static function del($id)
    {
        AuthGroupAccess::del($id);
        $data = self::whereIn('id', $id)->select();
        if (!$data) return false;
        self::destroy($id);
        return true;
    }
         //权限调用
           public static function group($id)
           {
             return self::where(['id' => $id])->find();
            }
      
}